package com.utils;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class QQUntil {


    public String getCodeUrl() throws IOException {
        try {
            Properties prop = new Properties();
            InputStream inputStream = QQUntil.class.getClassLoader().getResourceAsStream("/properties/qqConnectConfig.properties");
            prop.load(inputStream);
            String base_url = prop.getProperty("codeUrl");
            String response_type = prop.getProperty("response_type");
            String client_id = prop.getProperty("app_ID");
            String redirect_uri = prop.getProperty("redirect_URI");
            String state = prop.getProperty("state");
            String scope = prop.getProperty("scope");
            String display = prop.getProperty("pc");

            String Codeurl = base_url+"?response_type="+response_type+
                    "&client_id="+client_id+
                    "&redirect_uri="+redirect_uri+
                    "&state="+state+
                    "&scope="+scope+
                    "&display"+display;
            return Codeurl;
        }
        catch (IOException e) {
            return null;
        }
    }

    public String getTokenUrl(String code) throws IOException{
        try{
            Properties prop = new Properties();
            InputStream inputStream = QQUntil.class.getClassLoader().getResourceAsStream("/properties/qqConnectConfig.properties");
            prop.load(inputStream);
            String base_url = prop.getProperty("tokenUrl");
            String client_id = prop.getProperty("app_ID");
            String app_key = prop.getProperty("app_KEY");
            String redirect_uri = prop.getProperty("redirect_URI");
            String tokenUrl = base_url+"?grant_type=authorization_code"+
                    "&client_id="+client_id+
                    "&client_secret="+app_key+
                    "&code="+code+
                    "&redirect_uri="+redirect_uri;
            return tokenUrl;
        }
        catch (IOException e) {
            return null;
        }
    }

    public String getOpenIdUrl(String token){
        String url = "https://graph.qq.com/oauth2.0/me?"+
                "access_token="+token;
        return url;
    }

    public String getToken(String url) throws IOException {
        String result = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        if(httpEntity != null){
            result = EntityUtils.toString(httpEntity,"UTF-8");
            result = result.substring(13,45);
        }
        httpGet.releaseConnection();
        return result;
    }

    public String getOpenId(String url)throws IOException{
        JSONObject jsonObject = null;
        String result = null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        HttpResponse httpResponse = client.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        if(httpEntity != null){
            result = EntityUtils.toString(httpEntity,"UTF-8");
            result = result.substring(45,77);
        }
        httpGet.releaseConnection();
        return result;
    }

}
